package tsg.graphic;

public abstract class Gradient extends Color
{
	public Gradient(int position1, Color color1, int position2, Color color2)
	{
		this.calculateGradientMap(position1, color1, position2, color2);
	}
	
	public void calculateGradientMap(int position1, Color color1, int position2, Color color2)
	{
		int   distance   	= Math.abs(position1 - position2);
		float redDX			= 0.0f;
		float greenDX		= 0.0f;
		float blueDX		= 0.0f;
		Color startColor 	= color1;
		Color endColor   	= color2;
		Color currentColor	= null;
		
		
		_gradientMap     	= new Color[distance];
		_position1          = position1;
		_position2          = position2;
		
		if(position1 > position2)
		{
			startColor = color2;
			endColor   = color1;
			
			_position1 = position2;
			_position2 = position1;
		}
		
		float currentRed	= startColor.getRed();
		float currentGreen	= startColor.getGreen();
		float currentBlue	= startColor.getBlue();
		
		this.setRed		(startColor.getRed());
		this.setGreen	(startColor.getGreen());
		this.setBlue	(startColor.getBlue());
		
		redDX   = (endColor.getRed()   - startColor.getRed())   / distance;
		greenDX = (endColor.getGreen() - startColor.getGreen()) / distance;
		blueDX  = (endColor.getBlue()  - startColor.getBlue())  / distance;
		
		currentColor = startColor;
		for(int i=0; i<distance; i++)
		{
			_gradientMap[i] = currentColor;
			
			currentRed   = currentRed   + redDX;
			currentGreen = currentGreen + greenDX;
			currentBlue  = currentBlue  + blueDX;
			
			if(currentRed > 255)
			{
				currentRed = 255;
			}
			else if(currentRed < 0)
			{
				currentRed = 0;
			}
			
			if(currentGreen > 255)
			{
				currentGreen = 255;
			}
			else if(currentGreen < 0)
			{
				currentGreen = 0;
			}
			
			if(currentBlue > 255)
			{
				currentBlue = 255;
			}
			else if(currentBlue < 0)
			{
				currentBlue = 0;
			}
			
			currentColor = new Color((int)(currentRed), (int)(currentGreen), (int)(currentBlue));
		}
	}
	
	public Color[] getGradientMap()
	{
		return _gradientMap;
	}
	
	public int getPosition1()
	{
		return _position1;
	}
	
	public int getPosition2()
	{
		return _position2;
	}
	
	private Color[] _gradientMap = null;
	private int     _position1   = 0;
	private int     _position2   = 0;
}
